--[[ Poppy SAC:R plugin by Detective Squirrel
	
	Wall charge might miss (I dont know the right values)
	--Wall charge function was from a SAC Vayne Auto Condem plugin (I think it was Sida's)
	--Option to keep Heroic Charge for stunning on a wall or not
	
	--Use Heroic Charge to Interrupt spells (VERY buggy)
	
	Run away button for lazy people (Default X)
	--Will move to mouse and cast W
	
	Ulti
	--Casts ulti when just out of auto attack range
	
	There is next to no logic for combos as i just wanted to write something and play with it for the first time
	
	Credits for the Interrupter from Toy

--]]

require "MapPosition"
require "VPrediction"
require "Prodiction"
require "Collision"

local QReady, WReady, EReady, RReady = nil, nil, nil, nil
local eDist, rDist, RangeMeele = 525, 900, 200

local wallStunStats = {spellKey = _E, range = 525, speed = 1, delay = 0, width=10}
local mapPosition = MapPosition()     

local enemyHeroes
local ToInterrupt = {}
local InterruptList = {
    { charName = "Caitlyn", spellName = "CaitlynAceintheHole"},
    { charName = "FiddleSticks", spellName = "Crowstorm"},
    { charName = "FiddleSticks", spellName = "DrainChannel"},
    { charName = "Galio", spellName = "GalioIdolOfDurand"},
    { charName = "Karthus", spellName = "FallenOne"},
    { charName = "Katarina", spellName = "KatarinaR"},
    { charName = "Lucian", spellName = "LucianR"},
    { charName = "Malzahar", spellName = "AlZaharNetherGrasp"},
    { charName = "MissFortune", spellName = "MissFortuneBulletTime"},
    { charName = "Nunu", spellName = "AbsoluteZero"},
    { charName = "Pantheon", spellName = "Pantheon_GrandSkyfall_Jump"},
    { charName = "Shen", spellName = "ShenStandUnited"},
    { charName = "Urgot", spellName = "UrgotSwap2"},
    { charName = "Varus", spellName = "VarusQ"},
    { charName = "Warwick", spellName = "InfiniteDuress"}
}

function PluginOnLoad()
	AutoCarry.SkillsCrosshair.range = 650
	AutoCarry.PluginMenu:addParam("eRange", "Draw E Range", SCRIPT_PARAM_ONOFF, true)
	AutoCarry.PluginMenu:addParam("rRange", "Draw R Range", SCRIPT_PARAM_ONOFF, true)
	AutoCarry.PluginMenu:addParam("eInterrupt", "Use Heroic Charge to Interrupt", SCRIPT_PARAM_ONOFF, true)
	AutoCarry.PluginMenu:addParam("wallStunCarry", "Heroic Charge into Wall with AutoCarry", SCRIPT_PARAM_ONOFF, true)
	AutoCarry.PluginMenu:addParam("keepEForStun", "Keep Heroic Charge for wall stun", SCRIPT_PARAM_ONOFF, true)
	AutoCarry.PluginMenu:addParam("runAway", "Run Away", SCRIPT_PARAM_ONKEYDOWN, false, 88)
	
	
	enemyHeroes = GetEnemyHeroes()

    for _, enemy in pairs(enemyHeroes) do
		for _, champ in pairs(InterruptList) do
			if enemy.charName == champ.charName then
				table.insert(ToInterrupt, champ.spellName)
			end
		end
	end
	
end

function PluginOnTick()
	myCoolDowns()
	
	if AutoCarry.MainMenu.AutoCarry then
		Combo()
	end
	if AutoCarry.PluginMenu.runAway then
		runAway()
	end

	if myHero.dead then return end
	local stunReady = (AutoCarry.PluginMenu.wallStunCarry and AutoCarry.MainMenu.AutoCarry)
	if stunReady and EReady then
		local target = AutoCarry.GetAttackTarget()
		wallStun(target)
	end
	
end

function PluginOnDraw()
	if not myHero.dead and AutoCarry.PluginMenu.eRange then
		DrawCircle(myHero.x, myHero.y, myHero.z, eDist, 0x00FFFF)
	end
	if not myHero.dead and AutoCarry.PluginMenu.rRange then
		DrawCircle(myHero.x, myHero.y, myHero.z, rDist, 0x00FFFF)
	end
end

function myCoolDowns()
	QReady = (myHero:CanUseSpell(_Q) == READY)
	WReady = (myHero:CanUseSpell(_W) == READY)
	EReady = (myHero:CanUseSpell(_E) == READY)
	RReady = (myHero:CanUseSpell(_R) == READY)
end

function Combo()
	local Target = AutoCarry.GetAttackTarget()

	if ValidTarget(Target) then
		local Distance = GetDistance(Target)
		
		-- Cast R
		if RReady and Distance <= eDist then
			CastSpell(_R, Target)
		end
		
		-- Cast Q
		if QReady and Distance <= RangeMeele then
			CastSpell(_Q)
		end
		
		-- Cast E
		if EReady and Distance <= eDist then 
			if AutoCarry.PluginMenu.keepEForStun then
				wallStun(target)
			else
				CastSpell(_E, Target)
			end
		end
		
		-- Cast W
		if WReady and Distance <= RangeMeele*2 then
			CastSpell(_W)
		end
	end
end


-- 
function wallStun(target)
	if ValidTarget(target, 525) then
		local targetPos = AutoCarry.GetPrediction(wallStunStats, target)
		if not targetPos then return end
		local pushPos = GetDistance(targetPos) > 50 and targetPos + (Vector(targetPos) - myHero):normalized()*450 or nil
		if pushPos and targetPos then
			local enemyPoint = Point(targetPos.x, targetPos.z)
			local stunPoint = Point(pushPos.x, pushPos.z)
			local lineSegment = LineSegment(enemyPoint, stunPoint)
			local wall = mapPosition:intersectsWall(lineSegment)
			if wall then
				CastSpell(_E, target)
			end
		end
	end
end

function runAway()
	myHero:MoveTo(mousePos.x, mousePos.z) 
	if WReady then
		CastSpell(_W)
	end
end

function PluginOnProcessSpell(unit, spell)
	if #ToInterrupt > 0 and AutoCarry.PluginMenu.eInterrupt and EReady then
		for _, ability in pairs(ToInterrupt) do
			if spell.name == ability and unit.team ~= myHero.team then
				if eDist >= GetDistance(unit) then
					print("Interrupting Spell" ..spell.name)
					CastSpell(_E, unit)
				end
			end
		end
	end
end